package com.tanguy.user.ui.me.activity

import android.support.v7.widget.LinearLayoutManager
import com.tanguy.user.R
import com.tanguy.user.base.TabLayoutActivity
import com.tanguy.user.bean.CouponsBean
import com.tanguy.user.net.http.HttpManager
import com.tanguy.user.ui.me.adapter.CouponsAdapter
import com.tanguy.user.utlis.extend.requestSucceed
import org.jetbrains.anko.find

/**
 * Created by dab on 2017/10/10 0010 11:37
 * 优惠卷
 */
class CouponsActivity : TabLayoutActivity() {
    override fun getTabString(): ArrayList<String> = arrayListOf("未使用", "已使用")
    private val data = ArrayList<CouponsBean>()
    private var type = 1 //1表示未使用,2表示已使用
    private val couponsAdapter by lazy {
        CouponsAdapter(data).apply {
            setSwipeRefreshLayout(find(R.id.sr_refresh)) {
                loadData(it)
            }
        }
    }


    override fun initView() {
        super.initView()
        mRvContent.layoutManager = LinearLayoutManager(this)
        mRvContent.adapter = couponsAdapter

    }

    override fun onTabItemSelected(position: Int) {
        when (position) {
            0 -> type = 1
            1 -> type = 2
        }
        data.clear()
        loadData(1)
    }

    private fun loadData(page: Int) {
        HttpManager.getCouponsList(type, page)
                .requestSucceed(this) {
                    couponsAdapter.setLoadData(it)
                }

    }

}